package org.batatasframework.support.searchable.result;

import java.sql.ResultSet;

import org.batatasframework.support.database.spi.ResultWritable;
import org.batatasframework.support.database.sql.field.SelectFragment;
import org.batatasframework.support.database.values.FieldValue;
import org.batatasframework.support.database.values.FieldValueHolder;
import org.batatasframework.support.searchable.SimpleHeadInfo;


public class SearchResultHead extends SimpleHeadInfo implements ResultHead{
	protected final SelectFragment selectFragment;
	protected final FieldValueHolder valueHolder;
	
	public SearchResultHead(SelectFragment selectFragment){
		super(selectFragment.getTitle());
		this.selectFragment = selectFragment;
		this.valueHolder = selectFragment.getValueHolder();
	}
	
	@Override
	public boolean disabled() {
		return false;
	}
	
	private class SearchResultCell implements ResultCell{

		private FieldValue value;
		
		@Override
		public void read(ResultSet rs) throws Exception {
			value = valueHolder.read(rs);
		}

		@Override
		public void writeTo(ResultWritable resultWriter) throws Exception {
			resultWriter.append(value.targetValue());
		}
	}
	
	@Override
	public ResultCell newCell() {
		return new SearchResultCell();
	}
	
	@Override
	public String[] group() {
		return selectFragment.getGroup();
	}

	@Override
	public int compareTo(ResultHead o) {
		return ordinal() - o.ordinal();
	}

	@Override
	public int ordinal() {
		return 0;
	}
	
	@Override
	public void setRowspan(int rowspan) { 

		this.rowspan = rowspan;
	}

	@Override
	public void setColspan(int colspan) {

        this.colspan = colspan;
		
	}
}
